﻿using System;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace Mapenect
{
    public partial class FmMain : Form
    {
        Controller controller;

        #region GUI

        public FmMain()
        {
            InitializeComponent();
        }

        private void FmMain_Load(object sender, EventArgs e)
        {
            controller = new Controller(this,pbxOutput);
        }

        private void FmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (controller != null)
                controller.DisbandController();

        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            controller.IncreaseMotorPosition();
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            controller.DecreaseMotorPosition();
        }


        private void btnReset_Click(object sender, EventArgs e)
        {
            controller.ResetMotorPosition();
        }

        private void rbtDepth_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb = sender as RadioButton;

            if (rb == null)
                throw new Exception("No Radiobutton");

            if (rb.Checked)
            {
                if (rb.Tag != null)
                    controller.SetCapturingMode((string)rb.Tag);
            }
        }

        #endregion

        #region SystemLog

        private delegate void LogMsg(string msg);

        internal void WriteToLog(string msg)
        {
            if (lbxLog.InvokeRequired)
            {
                lbxLog.Invoke(new LogMsg(WriteToLog), new object[] { msg });
            }
            else
            {
                lbxLog.Items.Add(msg);
                lbxLog.SelectedIndex += 1;
            }
        }

        internal void btnSave_Click(object sender, EventArgs e)
        {
            sfdSaveLog.ShowDialog();
            StreamWriter x = new StreamWriter(sfdSaveLog.OpenFile());
            StringBuilder msg = new StringBuilder();
            foreach ( string line in lbxLog.Items)
            {
                msg.AppendLine(line);
            }
            x.Write(msg.ToString());
            x.Flush();
        }

        #endregion

        #region Store Stuff

        private void btnStorePose_Click(object sender, EventArgs e)
        {
            string name = Microsoft.VisualBasic.Interaction.InputBox("Name der Geste?");
            if (name == "")
            {
                WriteToLog("Ungültiger Name. Vorgang abgebrochen.");
                return;
            }
            controller.SaveGesture(name);
        }

        #endregion

        internal void LockGui()
        {
            this.pnGui.Enabled = false;
        }

        #region Testmethods

        private void btnStart_Click(object sender, EventArgs e)
        {
            controller.StartTest();

        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            controller.StopTest();
        }

        internal void setVectMetrLabel(string value)
        {
            if (lbVecMetr.InvokeRequired)
                lbVecMetr.Invoke(new Action(delegate() { lbVecMetr.Text = "VecMetr: " + value; }));
            else
                lbVecMetr.Text = "VecMetr: " + value;
        }

        internal void setDistLabel(string value)
        {
            if (lbDistMetr.InvokeRequired)
                lbDistMetr.Invoke(new Action(delegate() { lbDistMetr.Text = "DistMetr: " + value; }));
            else
                lbDistMetr.Text = "DistMetr: " + value;
        }

        internal void setAngleLabel(string value)
        {
            if (lbDistMetrAng.InvokeRequired)
                lbDistMetrAng.Invoke(new Action(delegate() { lbDistMetrAng.Text = "Winkel: " + value; }));
            else
                lbDistMetrAng.Text = "Winkel: " + value;
        }

        #endregion

    }
}

